package com.pactera.integral.service.impl;

import com.pactera.integral.common.jpa.criteria.Criteria;
import com.pactera.integral.common.jpa.criteria.Restrictions;
import com.pactera.integral.dao.AdditionalScoreDao;
import com.pactera.integral.entity.AdditionalScore;
import com.pactera.integral.service.AdditionalScoreService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("/additionalScoreService")
public class AdditionalScoreServiceImpl implements AdditionalScoreService {

	@Autowired
	private AdditionalScoreDao additionalScoreDao;

	/**
	 * 添加或修改积分项
	 *
	 * @param additionalScore
	 * @throws Exception
	 */
	@Override
	public void save(AdditionalScore additionalScore) throws Exception {
		AdditionalScore existingEntity = additionalScoreDao.findByCode(additionalScore.getCode());

		if (existingEntity != null) {
			if (!existingEntity.getId().equals(additionalScore.getId())) {
				//不同的ID发现同样的Code视为已存在

				throw new Exception("积分编码已存在!");
			} else {
				existingEntity.setCategory(additionalScore.getCategory());
				existingEntity.setEnabled(additionalScore.isEnabled());
				existingEntity.setResult(additionalScore.getResult());
				existingEntity.setScore(additionalScore.getScore());
				existingEntity.setScoreType(additionalScore.getScoreType());
				existingEntity.setDetail(additionalScore.getDetail());
				existingEntity.setRemark(additionalScore.getRemark());

				additionalScoreDao.save(existingEntity);
			}
		} else {
			additionalScore.setEnabled(true);
			additionalScoreDao.save(additionalScore);
		}
	}


	/**
	 * 删除积分向
	 *
	 * @param additionalScoreId
	 * @return Boolean
	 */
	@Override
	public Boolean delete(String additionalScoreId) {
		AdditionalScore additionalScore = additionalScoreDao.findById(additionalScoreId);
		additionalScore.setEnabled(false);
		additionalScoreDao.save(additionalScore);

		return Boolean.TRUE;
	}

	/**
	 * 积分向列表
	 *
	 * @param param
	 * @param page
	 * @return Page<AdditionalScore>
	 */
	@Override
	public Page<AdditionalScore> pageAdditionalScore(Map<String, Object> param, Pageable page) {
		Criteria<AdditionalScore> criteria = new Criteria<AdditionalScore>();
		if (param != null) {
			criteria.add(Restrictions.eq("enabled", 1));
			criteria.add(Restrictions.eq("scoreType", (String) param.get("scoreType")));
			criteria.add(Restrictions.like("code", (String) param.get("code")));
			criteria.add(Restrictions.eq("category", (String) param.get("category")));

		}
		String sort = (String) param.get("sort");
		String sortProp = (String) param.get("sortProp");
		if (StringUtils.isNotEmpty(sort) && StringUtils.isNotEmpty(sortProp)) {
			Sort.Direction direction = Sort.Direction.ASC;
			if ("descending".equalsIgnoreCase(sort)) {
				direction = Sort.Direction.DESC;
			}
			page = new PageRequest(page.getPageNumber(), page.getPageSize(), direction, sortProp);
		}
		Page<AdditionalScore> p = additionalScoreDao.findAll(criteria, page);
		return p;
	}

	/**
	 * 积分向明细
	 *
	 * @param id
	 * @return AdditionalScore
	 */
	@Override
	public AdditionalScore getAdditionalScore(String id) {

		return additionalScoreDao.findById(id);
	}

}
